package com.qianchang.microfinance.view.mine.bill;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.HtmlUtil;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.AgreementBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.register.AgreementActivity;
import com.qianchang.microfinance.view.register.AgreementApi;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 协议
 */
public class PayTonglianActivity extends BaseActivity {

    @BindView(R.id.tv_product_introduction)
    WebView tvProductIntroduction;
    private HtmlUtil htmlUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_agreement);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        WebSettings webSettings = tvProductIntroduction.getSettings();
        webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
//        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);//支持缩放
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //加载第三方页面时，我们没办法控制别人使用http还是https，如果我们服务器和对方使用不同的协议，就需要做这个处理了。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // 加载需要显示的网页
//        webView.addJavascriptInterface(new JavascriptInterface(), "classNameBeExposedInJs");
        tvProductIntroduction.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        htmlUtil = new HtmlUtil();
        htmlUtil.init(DMConstant.Config.BASE_URL);
    }

//        0 现金贷借款协议
//        1 消费贷借款协议
//        2 抵押贷借款协议
//        3 租房分期借款协议
//        4 学生贷借款协议
//        5 业主贷借款协议
//        6 用户服务协议
//        7 个人征信查询授权书
//        8 委托扣款授权书
//        9 数字证书服务协议
//        10 第三方风控服务授权书
//        100 芝麻信用授权页

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if(null != bundle) {
            HttpParams params = new HttpParams();
            params.put("projectId", bundle.getString("projectId")); //项目id
            params.put("period", bundle.getString("period")); //产品的哪一期
            params.put("userId", bundle.getString("userId"));
            /**
             * 订单类型：1：开通会员 2：充值金币  3：充值余额  4：手工提现 5：自动提现 6.余额正常还款 7.银行卡正常还款
             * 8.余额逾期还款 9.银行卡逾期还款 10.代还
             */
            params.put("rechargeType", bundle.getString("rechargeType")); // 类型 micoolWong
            params.put("principal", bundle.getString("principal")); //本金
            params.put("interest", bundle.getString("interest")); //利息
            params.put("overdue", bundle.getString("overdue")); //逾期费用

            DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.REPAY_BANK_H5, params, new RequestCallBack<String>() {
                @Override
                public void onSuccess(JsonResult<String> response) {
                    dismiss();
                    if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                        //接收到通联url
                        String url = "https://cashier.allinpay.com/mobilepayment/mobile/SaveMchtOrderServlet.action?"+response.getObject();
                        tvProductIntroduction.loadUrl(url);
                        setTitleName("通联支付");
                        tvProductIntroduction.setWebChromeClient(new WebChromeClient());
                        tvProductIntroduction.setWebViewClient(new WebViewClient(){
                            @Override
                            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                                super.onPageStarted(view, url, favicon);
                                String strs = "webapp/qianpen/redirect/zhima";
                                String strUrl = url;
                                if(strUrl.contains(strs) == true){
                                    PayTonglianActivity.this.finish();
                                }
                            }

                            @Override
                            public void onPageFinished(WebView view, String url) {
                                super.onPageFinished(view, url);
                                //http://19411b432a.iok.la:22643/user/qianpen/v/creditZhiMaH5CallBack.do?openIdInfo=eyJzdGF0ZSI6IjAwMTAxNTExNzY0NjI3Mzg5MTAxIiwib3BlbklkIjoiMjY4ODAwNjM5NzM2ODQ3NDE4MzI3MzQ3OTgyIn0=
                                String strUrl = url;
                            }
                        });
                        tvProductIntroduction.getSettings().setJavaScriptEnabled(true);
                        tvProductIntroduction.loadUrl(url);
                    } else {
                        ToastUtil.getInstant().show(PayTonglianActivity.this, response.getDescription());
                    }
                }

                @Override
                public void onFailure(String msg) {
                    dismiss();
                    ToastUtil.getInstant().show(PayTonglianActivity.this, msg);// getString(R.string.msg_indentity_code_send_fail));
                }

                @Override
                public void onConnectFailure(String msg) {
                    ToastUtil.getInstant().show(PayTonglianActivity.this, msg);
                }
            });
        }else{
            ToastUtil.getInstant().show(PayTonglianActivity.this, "参数错误！");
        }
    }

}
